<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="150px" size="small">
      <el-form-item label="部门名称" prop="organizationName">
        <el-input v-model="dataForm.organizationName" placeholder="部门名称"></el-input>
      </el-form-item>
      <el-form-item label="上级部门" prop="parentOrgName">
        <el-popover
          ref="menuListPopover"
          placement="bottom-start"
          trigger="click">
          <div style="height: 350px">
            <el-scrollbar style="height: 100%;width: 100%">
              <el-tree
                :data="menuList"
                :props="menuListTreeProps"
                node-key="organizationId"
                ref="menuListTree"
                @current-change="menuListTreeCurrentChangeHandle"
                :default-expand-all="true"
                :highlight-current="true"
                :expand-on-click-node="false">
              </el-tree>
            </el-scrollbar>
          </div>
        </el-popover>
        <el-input v-model="dataForm.parentOrgName" v-popover:menuListPopover :readonly="true" placeholder="点击选择所属区域" class="menu-list__input"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false" size="small">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" size="small">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { treeDataTranslate } from '@/utils'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          organizationId: 0,
          organizationName: '',
          parentOrgId: '-1',
          parentOrgName:''
        },
        menuList: [],
        menuListTreeProps: {
          label: 'organizationName',
          children: 'children'
        },
        dataRule: {
          organizationName: [
            { required: true, message: '部门名称不能为空', trigger: 'blur' }
          ],
          parentOrgId: [
            { required: true, message: '上级部门ID', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.organizationId = id || 0

        this.$api.okhttp({
          url: this.$http.adornUrl('/ecs/ecsorganizationinfo/select'),
          method: 'get',
          params: this.$http.adornParams({
            'organizationId': this.dataForm.organizationId,
            "customerId": this.$util.cookies.get('customer_id'),
          })
        }).then((data) => {
          this.menuList = treeDataTranslate(data.menuList, 'organizationId','parentOrgId')
      }).then(() => {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
      })
      }).then(() => {
          if (this.dataForm.organizationId) {
            this.$api.okhttp({
              url: this.$http.adornUrl(`/ecs/ecsorganizationinfo/info/${this.dataForm.organizationId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 0) {
                this.dataForm.organizationName = data.ecsorganizationinfo.organizationName
                this.dataForm.parentOrgId = data.ecsorganizationinfo.parentOrgId
              this.menuListTreeSetCurrentNode()
              }
            })
          }else{
            this.menuListTreeSetCurrentNode()
       }
        })
      },
      // 菜单树选中
      menuListTreeCurrentChangeHandle (data, node) {
        this.dataForm.parentOrgId = data.organizationId
        this.dataForm.parentOrgName = data.organizationName
      },
      // 菜单树设置当前选中节点
      menuListTreeSetCurrentNode () {
        this.$refs.menuListTree.setCurrentKey(this.dataForm.parentOrgId)
        this.dataForm.parentOrgName = (this.$refs.menuListTree.getCurrentNode() || {})['organizationName']
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$api.okhttp({
              url: this.$http.adornUrl(`/ecs/ecsorganizationinfo/${!this.dataForm.organizationId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'organizationId': this.dataForm.organizationId || undefined,
                'organizationName': this.dataForm.organizationName,
                'parentOrgId': this.dataForm.parentOrgId,
                "customerId": this.$util.cookies.get('customer_id'),
              })
            }).then((data) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
